package com.tencreat.mall.controller.admin.im;

import com.tencreat.common.core.HttpResult;
import com.tencreat.common.enums.ActionMessageType;
import com.tencreat.common.exception.CustomException;
import com.tencreat.common.utils.RabbitSendUtils;
import com.tencreat.common.utils.uuid.IdUtils;
import com.tencreat.domain.im.ImCustomer;
import com.tencreat.domain.im.dto.OpenIMJoinGroupDTO;
import com.tencreat.domain.im.dto.OpenIMLoginDTO;
import com.tencreat.domain.im.vo.OpenIMResultVo;
import com.tencreat.domain.im.vo.OpenIMTokenVo;
import com.tencreat.service.im.IImCustomerService;
import com.tencreat.service.im.OpenIMApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * IM  客服Controller
 *
 * @author tencreat
 * @company 深圳腾创互联科技有限公司
 * @link https://tencreat.com
 * @since 2023-11-22
 */
@Api(tags = "IM  客服api")
@RestController
@RequestMapping("/mall/im/customer")
@RequiredArgsConstructor
public class ImCustomerController extends BaseController {

    private final IImCustomerService imCustomerService;
    private final RabbitSendUtils rabbitSendUtils;

    @ApiOperation(value = "查询IM  客服列表")
    @PostMapping("/list")
        HttpResult<IPage<ImCustomer>> list(@RequestBody ImCustomer imCustomer) {
        IPage<ImCustomer> iPage = generatePage(imCustomer);
        return HttpResult.ok(imCustomerService.page(iPage, imCustomer));
    }
    
    @ApiOperation(value = "导出IM  客服列表")
    @Log(title = "IM  客服", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    AjaxResult export(@RequestBody ImCustomer imCustomer) {
        List<ImCustomer> list = imCustomerService.page(getMaxPage(), imCustomer).getRecords();
        ExcelUtil<ImCustomer> util = new ExcelUtil<>(ImCustomer. class);
        return util.exportExcel(list, "IM  客服");
    }

    @ApiOperation(value = "获取IM  客服详细信息")
    @GetMapping(value = "/getInfo/{id}")
    HttpResult<ImCustomer> getInfo(@PathVariable("id") Long id) {
        return HttpResult.ok(imCustomerService.getById(id));
    }

    @ApiOperation(value = "新增IM  客服")
    @Log(title = "IM  客服", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    HttpResult<Boolean> add(@RequestBody @Validated ImCustomer imCustomer) {
        imCustomer.setImId(IdUtils.fastSimpleUUID());
        boolean save = imCustomerService.save(imCustomer);
        if(save){
            // 发送创建客服消息队列
            rabbitSendUtils.sendActionMessage(null, ActionMessageType.CUSTOMER_CREATE, imCustomer);
        }
        return to(save);
    }

    @ApiOperation(value = "修改IM  客服")
    @Log(title = "IM  客服", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    HttpResult<Boolean> edit(@RequestBody @Validated ImCustomer imCustomer) {
        return to(imCustomerService.updateById(imCustomer));
    }

    @ApiOperation(value = "删除IM  客服")
    @Log(title = "IM  客服", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    HttpResult<Boolean> remove(@RequestBody List<Long> ids) {
        return to(imCustomerService.removeByIds(ids));
    }
}